<?php
App::import('Vendor', 'PHPExcel', array('file' => 'PHPExcel.php'));
App::import('Vendor', 'PHPExcel_Reader_Excel2007', array('file' => 'PHPExcel/Excel2007.php'));
App::import('Vendor', 'PHPExcel_IOFactory', array('file' => 'PHPExcel/PHPExcel/IOFactory.php'));
App::uses('AppController', 'Controller');

/**
 * Users Controller
 *
 * @property User $User
 * @property PaginatorComponent $Paginator
 */
class ReportesController extends AppController {

    public $layout = 'recargas';
    public $uses = array('Facturacione','User');

    public function general()
    {
        //debug($this->request->data);exit;
        if(!empty($this->request->data))
        {
            $fecha_ini = $this->request->data['Reportes']['fecha_ini'];
            $fecha_fin = $this->request->data['Reportes']['fecha_fin'];
            $transacciones = $this->Facturacione->find('all', array(
                'recursive'=>-1, 
                'conditions' => array(
                'Facturacione.fecha_transaccion >=' => $this->request->data['Reportes']['fecha_ini'],
                'Facturacione.fecha_transaccion <=' => $this->request->data['Reportes']['fecha_fin']
                ),           
            ));
        }
        else{
            $transacciones = $this->Facturacione->find('all', array(
                'recursive'=>-1,            
            ));
        }
        
        $this->set(compact('transacciones','fecha_ini','fecha_fin'));
    }
    public function exportageneral($fecha_ini = null,$fecha_fin = null)
    {
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename="Libro de Ventas.xlsx"');
        header('Cache-Control: max-age=0');
        $borders = array(
              'borders' => array(
                'allborders' => array(
                  'style' => PHPExcel_Style_Border::BORDER_THIN,
                  'color' => array('argb' => 'FFFF0000')
                )
              ),
              'font' => array(
                    'size' => 12,
                  'color' => array('argb' => 'FFFF0000')
                
              ),
            );
        //$objPHPExcel->getActiveSheet()->getStyle('A1:B1')->applyFromArray($borders);
        $prueba = new PHPExcel($objPHPExcel); 
        //$prueba->addCellStyleXf($objPHPExcel)
        $prueba-> getActiveSheet () -> getColumnDimension ( 'A' ) ->setWidth(15);
        $prueba-> getActiveSheet () -> getColumnDimension ( 'B' ) ->setWidth(15);
        $prueba-> getActiveSheet () -> getColumnDimension ( 'C' ) ->setWidth(20);
        $prueba-> getActiveSheet () -> getColumnDimension ( 'D' ) ->setWidth(15);
        $prueba-> getActiveSheet () -> getColumnDimension ( 'E' ) ->setWidth(20);
        $prueba-> getActiveSheet () -> getColumnDimension ( 'F' ) ->setWidth(18);
        $prueba-> getActiveSheet () -> getColumnDimension ( 'G' ) ->setWidth(14);
        $prueba-> getActiveSheet () -> getColumnDimension ( 'H' ) ->setWidth(15);
        $prueba-> getActiveSheet () -> getColumnDimension ( 'I' ) ->setWidth(15);
        $prueba->getActiveSheet()->getStyle('A1:I1')->applyFromArray($borders);
        $prueba->setActiveSheetIndex(0)->setCellValue("A1","FECHA");
        $prueba->setActiveSheetIndex(0)->setCellValue("B1","NIT/CI");
        $prueba->setActiveSheetIndex(0)->setCellValue("C1","CLIENTE");
        $prueba->setActiveSheetIndex(0)->setCellValue("D1","FACTURA Nro");
        $prueba->setActiveSheetIndex(0)->setCellValue("E1","COD. CONTROL");
        $prueba->setActiveSheetIndex(0)->setCellValue("F1","Nr AUTORIZACION");
        $prueba->setActiveSheetIndex(0)->setCellValue("G1","IMPORTE NETO");
        $prueba->setActiveSheetIndex(0)->setCellValue("H1","DEBITO FISCAL");
        $prueba->setActiveSheetIndex(0)->setCellValue("I1","ESTADO");
        
        $prueba->getActiveSheet()->setTitle("Libro de Ventas"); 
        $cont = 1;
        //debug($alumnos);exit;
        
        $transacciones = $this->Facturacione->find('all', array(
                'recursive'=>-1, 
                'conditions' => array(
                'Facturacione.fecha_transaccion >=' => $fecha_ini,
                'Facturacione.fecha_transaccion <=' => $fecha_fin
                ),           
            ));
        
        foreach($transacciones as $a)
        {
            $cont++;
            $prueba->setActiveSheetIndex(0)->setCellValue("A".$cont,$a['Facturacione']['fecha_transaccion']);
            $prueba->setActiveSheetIndex(0)->setCellValue("B".$cont,$a['Facturacione']['nit_ci_cliente']);
            $prueba->setActiveSheetIndex(0)->setCellValue("C".$cont,$a['Facturacione']['nombre_cliente']);
            $prueba->setActiveSheetIndex(0)->setCellValue("D".$cont,$a['Facturacione']['numero_factura']);
            $prueba->setActiveSheetIndex(0)->setCellValue("E".$cont,$a['Facturacione']['codigo_control']);
            $prueba->setActiveSheetIndex(0)->setCellValue("F".$cont,$a['Facturacione']['numero_autorizacion']);
            $prueba->setActiveSheetIndex(0)->setCellValue("G".$cont,$a['Facturacione']['monto_transaccion']);
            $prueba->setActiveSheetIndex(0)->setCellValue("H".$cont,$a['Facturacione']['monto_transaccion'] * 0.13);
            if($a['Facturacione']['estado'] == 0)
            {
                $prueba->setActiveSheetIndex(0)->setCellValue("I".$cont,"ANULADO");
            }
            else{
                $prueba->setActiveSheetIndex(0)->setCellValue("I".$cont,"");
            }
        }
        
        $objWriter = PHPExcel_IOFactory::createWriter($prueba, 'Excel2007'); 
        $objWriter->save('php://output'); 
        
        
        //$this->redirect("../webroot/files/listaalumnos.xlsx", null, false);
    }
    
    public function gzonas()
    {
        $reporte =  $this->User->find('all');
        $this->set(compact('reporte')); 
        //debug($reporte);exit;
    }
    
    public function granking()
    {
        $repor =  $this->User->find('all');
        $this->set(compact('repor')); 
        //debug($repor);exit;
    }  
    
    public function gventas()
    {
        
    }
}